/**
 * Created by Freeman on 2016/7/5.
 */
var Comment = require('../../models/comment.model'),
    Blog = require('../../models/article.model');


exports.getFrontCommentList = function (req,res,next) {
    var aid = req.params.id;
    Comment.find({aid:aid,status:{$eq:1}})
        .sort('created')
        .populate({
            path: 'user_id',
            select: 'nickname avatar'
        })
        .exec()
        .then(function (commentList) {
            return res.status(200).json({data:commentList});
        }).then(null,function (err) {
            return next(err);
        });
}

exports.addNewComment = function (req,res,next) {
    var aid = req.body.aid;
    var content = req.body.content;
    var userId = req.user._id;

    var error_msg;
    if(!aid){
        error_msg = '缺少必须参数';
    }else if(!content || content == ''){
        error_msg = "评论内容不能为空";
    }
    if(error_msg){
        return res.status(422).send({error_msg:error_msg});
    }

    Comment.createAsync({
        aid:aid,
        content:content,
        user_id:userId
    }).then(function (result) {
        var comment = result.toObject();
        comment.user_id = {
            _id:req.user._id,
            nickname:req.user.nickname,
            avatar:req.user.avatar
        }
        Blog.findByIdAndUpdateAsync(aid,{$inc:{comment_count:1}});
        return res.status(200).json({success:true,data:comment});
    }).catch(function (err) {
        return next(err);
    });
};

exports.addNewReply = function (req,res,next) {
    var cid = req.params.id;
    if(!req.body.content || req.body.content == ''){
        return res.status(422).send({error_msg:"回复内容不能为空"});
    }
    var reply = req.body;
    reply.user_info = {
        id:req.user._id,
        nickname:req.user.nickname
    }
    reply.created = new Date();
    Comment.findByIdAndUpdateAsync(cid,{"$push":{"replys":reply}},{new:true})
        .then(function (result) {
            return res.status(200).json({success:true,data:result.replys});
        }).catch(function (err) {
            return next(err);
        });

};